﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp7
{
    class Program
    {
        abstract class Shape
        {
            public abstract double  Area();
            public abstract void Init();
        }
        class Rectangle : Shape
        {
            int width;
            int height;
            public Rectangle()
            {
                Init();
            }
            public override double Area()
            {
                return width * height;
            }
            public override void Init()
            {
                Console.WriteLine("随机生成的长方形长宽分别为：");
                var seed = Guid.NewGuid().GetHashCode();
                Random a = new Random(seed);
                width = a.Next(1,10);
                height = a.Next(1,10);
                while (width ==height)
                {
                    width = a.Next(1, 10);
                    height = a.Next(1, 10);
                }
                Console.WriteLine(width + ", " + height);
            }
        }
        class Square : Shape
        {
            int width;
            public Square()
            {
                Init();
            }
            public override double Area()
            {
                return width * width;
            }
            public override void Init()
            {
                Console.WriteLine("随机生成的正方形边长为：");
                var seed = Guid.NewGuid().GetHashCode();
                Random b = new Random(seed);
                width = b.Next(1,10);
                Console.WriteLine(width);
            }
        }
        class Triangle : Shape
        {
            double x;
            double y;
            double z;
            double p;
            double s;
            public Triangle()
            {
                Init();
            }
            public override double Area()
            {
                p = (x + y + z) / 2;
                s = p * (p - x) * (p - y) * (p - z);
                return Math.Sqrt(s);
            }
            public override void Init()
            {
                var seed = Guid.NewGuid().GetHashCode();
                Random c = new Random(seed);
                x = c.Next(2, 10);
                y = c.Next(2, 10);
                z = c.Next(2, 10);
                while (x + y <= z || x + z <= y || y + z <= x)
                {
                    x = c.Next(2, 10);
                    y = c.Next(2, 10);
                    z = c.Next(2, 10);
                }
                Console.WriteLine("随机生成的三角形三边分别为：");
                Console.WriteLine(x + "," + y + "," + z);
            }
        }
        class Factory
        {
            public static  Shape CreatFunction(int name)
            {
                switch (name)
                {
                    case 1: return new Rectangle();
                    case 2: return new Square();
                    case 3: return new Triangle();
                }
                return null;
            }
        }
        static void Main(string[] args)
        {
            int i;
            int[] name = new int[10];
            double[] area = new double[10];
            double sumArea=0;
            Random d = new Random();
            for (i = 0; i < 10; i++)
            {
                name[i] = d.Next(1, 4);
                Shape shape = Factory.CreatFunction(name[i]);
                Console.WriteLine("该图形的面积为：" + shape.Area());
                Console.WriteLine();
                area[i] = shape.Area();
                sumArea = sumArea + shape.Area();
            }
            Console.WriteLine("十个随机图形的面积总和为：" + sumArea);
            Console.ReadKey();
        }
    }
}
